{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "Using TensorFlow backend.\n",
      "/anaconda3/envs/benchmarking/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:526: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint8 = np.dtype([(\"qint8\", np.int8, 1)])\n",
      "/anaconda3/envs/benchmarking/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:527: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_quint8 = np.dtype([(\"quint8\", np.uint8, 1)])\n",
      "/anaconda3/envs/benchmarking/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:528: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint16 = np.dtype([(\"qint16\", np.int16, 1)])\n",
      "/anaconda3/envs/benchmarking/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:529: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_quint16 = np.dtype([(\"quint16\", np.uint16, 1)])\n",
      "/anaconda3/envs/benchmarking/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:530: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  _np_qint32 = np.dtype([(\"qint32\", np.int32, 1)])\n",
      "/anaconda3/envs/benchmarking/lib/python3.6/site-packages/tensorflow/python/framework/dtypes.py:535: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)type'.\n",
      "  np_resource = np.dtype([(\"resource\", np.ubyte, 1)])\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "import scipy\n",
    "import os\n",
    "from keras.models import Sequential\n",
    "from keras.layers import Dense, Dropout, Flatten\n",
    "from keras.layers import Conv2D, MaxPooling2D\n",
    "from keras.optimizers import SGD\n",
    "from keras import backend as K\n",
    "K.set_image_dim_ordering('th')\n",
    "from keras.utils import np_utils\n",
    "#from sklearn.cross_validation import StratifiedKFold"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/Volumes/ADATA_HV620/indepented_projects/Classification-of-Hyperspectral-Image\n"
     ]
    }
   ],
   "source": [
    "PATH = os.getcwd()\n",
    "print (PATH)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Load the Global values (windowSize, numPCAcomponents, testRatio) from the text file global_variables.txt\n",
    "myFile = open('global_variables.txt', 'r') \n",
    "file = myFile.readlines()[:]\n",
    "\n",
    "\n",
    "for line in file:\n",
    "\n",
    "    if line[0:3] == \"win\":\n",
    "\n",
    "        ds = line.find('=')\n",
    "        windowSize = int(line[ds+1:-1],10)\n",
    "\n",
    "    elif line[0:3] == \"num\":\n",
    "\n",
    "        ds = line.find('=')\n",
    "        numPCAcomponents = int(line[ds+2:-1],10)\n",
    "\n",
    "    else:\n",
    "\n",
    "        ds = line.find('=')\n",
    "        testRatio = float(line[ds+1:])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Global Variables\n",
    "#windowSize = 5\n",
    "#numPCAcomponents = 30\n",
    "#testRatio = 0.25"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Load Training Dataset"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train = np.load(\"X_trainPatches_\" + str(windowSize) + \"PCA\" + str(numPCAcomponents) + \"testRatio\" + str(testRatio)  + \".npy\")\n",
    "\n",
    "y_train = np.load(\"y_trainPatches_\" + str(windowSize) + \"PCA\" + str(numPCAcomponents) + \"testRatio\" + str(testRatio) + \".npy\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Reshape into (numberofsamples, channels, height, width)\n",
    "X_train = np.reshape(X_train, (X_train.shape[0],X_train.shape[3], X_train.shape[1], X_train.shape[2]))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "# convert class labels to on-hot encoding\n",
    "y_train = np_utils.to_categorical(y_train)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(30, 5, 5)\n"
     ]
    }
   ],
   "source": [
    "# Define the input shape \n",
    "input_shape= X_train[0].shape\n",
    "print(input_shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "# number of filters\n",
    "C1 = 3*numPCAcomponents"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /anaconda3/envs/benchmarking/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py:263: colocate_with (from tensorflow.python.framework.ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Colocations handled automatically by placer.\n",
      "WARNING:tensorflow:From /anaconda3/envs/benchmarking/lib/python3.6/site-packages/keras/backend/tensorflow_backend.py:3445: calling dropout (from tensorflow.python.ops.nn_ops) with keep_prob is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Please use `rate` instead of `keep_prob`. Rate should be set to `rate = 1 - keep_prob`.\n"
     ]
    }
   ],
   "source": [
    "# Define the model\n",
    "model = Sequential()\n",
    "\n",
    "model.add(Conv2D(C1, (3, 3), activation='relu', input_shape=input_shape))\n",
    "model.add(Conv2D(3*C1, (3, 3), activation='relu'))\n",
    "model.add(Dropout(0.25))\n",
    "\n",
    "\n",
    "\n",
    "model.add(Flatten())\n",
    "model.add(Dense(6*numPCAcomponents, activation='relu'))\n",
    "model.add(Dropout(0.5))\n",
    "model.add(Dense(16, activation='softmax'))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [],
   "source": [
    "sgd = SGD(lr=0.0001, decay=1e-6, momentum=0.9, nesterov=True)\n",
    "model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "WARNING:tensorflow:From /anaconda3/envs/benchmarking/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py:3066: to_int32 (from tensorflow.python.ops.math_ops) is deprecated and will be removed in a future version.\n",
      "Instructions for updating:\n",
      "Use tf.cast instead.\n",
      "Epoch 1/15\n",
      "29685/29685 [==============================] - 7s 235us/step - loss: 2.4806 - acc: 0.2293\n",
      "Epoch 2/15\n",
      "29685/29685 [==============================] - 7s 221us/step - loss: 1.8380 - acc: 0.4680\n",
      "Epoch 3/15\n",
      "29685/29685 [==============================] - 7s 222us/step - loss: 1.3506 - acc: 0.5984\n",
      "Epoch 4/15\n",
      "29685/29685 [==============================] - 7s 242us/step - loss: 1.0505 - acc: 0.6706\n",
      "Epoch 5/15\n",
      "29685/29685 [==============================] - 7s 237us/step - loss: 0.8753 - acc: 0.7153\n",
      "Epoch 6/15\n",
      "29685/29685 [==============================] - 6s 202us/step - loss: 0.7567 - acc: 0.7478\n",
      "Epoch 7/15\n",
      "29685/29685 [==============================] - 6s 201us/step - loss: 0.6826 - acc: 0.7710\n",
      "Epoch 8/15\n",
      "29685/29685 [==============================] - 6s 213us/step - loss: 0.6224 - acc: 0.7878\n",
      "Epoch 9/15\n",
      "29685/29685 [==============================] - 8s 253us/step - loss: 0.5679 - acc: 0.8062\n",
      "Epoch 10/15\n",
      "29685/29685 [==============================] - 7s 228us/step - loss: 0.5254 - acc: 0.8212\n",
      "Epoch 11/15\n",
      "29685/29685 [==============================] - 7s 224us/step - loss: 0.4873 - acc: 0.8354\n",
      "Epoch 12/15\n",
      "29685/29685 [==============================] - 7s 235us/step - loss: 0.4569 - acc: 0.8470\n",
      "Epoch 13/15\n",
      "29685/29685 [==============================] - 7s 243us/step - loss: 0.4309 - acc: 0.8557\n",
      "Epoch 14/15\n",
      "29685/29685 [==============================] - 7s 224us/step - loss: 0.4049 - acc: 0.8635\n",
      "Epoch 15/15\n",
      "29685/29685 [==============================] - 7s 237us/step - loss: 0.3744 - acc: 0.8782 0s - loss: 0.3757 - acc\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<keras.callbacks.History at 0x127f95518>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "model.fit(X_train, y_train, batch_size=32, epochs=15)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [],
   "source": [
    "import h5py\n",
    "from keras.models import load_model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "model.save('my_model' + str(windowSize) + 'PCA' + str(numPCAcomponents) + \"testRatio\" + str(testRatio) + '.h5')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
